package com.example.util;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：
 * @date ：Created in 2022/12/13 8:39
 * @description：权限
 * @modified By：
 */
public class LoginInterceptor implements HandlerInterceptor {

    //请求方法前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        //1--放行，-----请求登录，退出登录，首页访问等请求
//        if(request.getSession().getAttribute("worker")!=null||requestURI.contains("login")||
//            requestURI.equals("/")||requestURI.equals("/worker/logout"))
//        {
//            //放行请求
//            return true;
//        }
//        String[] canURL = {
//                "/department",
//                "/vacate",
//                "/worker",
//        };
//       //2--未登录情况下，访问请求数据库数据的话，则触发跳转
//        for (String s : canURL) {
//            if(requestURI.contains(s)){
//                //返回"NOTLOGIN"标志，利用前端js跳转到登录页面
//                response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//                return false;
//            }
//        }

        //3--如果为js等静态资源，则放行
        return true;
    }

    //请求方法后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    //资源释放后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
